package org.smoozik.app.common.partition.component;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Rectangle;

import org.smoozik.app.common.partition.PartitionValues;
import org.smoozik.services.musictheory.Figure;
import org.smoozik.services.musictheory.Partition;
import org.smoozik.services.musictheory.Token;

@SuppressWarnings("serial")
public abstract class UIMusicComponent extends Rectangle implements
		Comparable<UIMusicComponent> {
	protected Token token;
	protected UIPartition partition;
	protected boolean circled;
	String label;
	// protected UIMeasure measure;
	private Color labelColor;

	abstract public void paint(Graphics g);

	public UIMusicComponent(UIPartition partition) {
		this.partition = partition;
		width = PartitionValues.getWidth();
		height = 4 * width;
	}

	// create the music component according to the token
	// if the token does not have a UIPartition, this constructor creates the
	// uiPartition
	public UIMusicComponent(Token e) {
		this.token = e;
		width = PartitionValues.getWidth();
		height = 4 * width;
		Figure f = e.getFigure();
		if (f != null) {
			Partition p = f.getPartition();
			if (p instanceof UIPartition) {
				this.partition = (UIPartition) p;
			} else {
				this.partition = new UIPartition(p);
			}
		}
	}

	// public UIMusicComponent(Token e, UIMeasure measure) {
	// this(measure);
	// this.token = e;
	// }

	// Getters setters

	public Color getLabelColor() {
		return labelColor;
	}

	public void setLabelColor(Color labelColor) {
		this.labelColor = labelColor;
	}

	// public UIMeasure getMeasure() {
	// return measure;
	// }
	//
	// public void setMeasure(UIMeasure measure) {
	// this.measure = measure;
	// }

	public String getLabel() {
		return label;
	}

	public void setLabel(String label) {
		this.label = label;
	}

	public boolean isCircled() {
		return circled;
	}

	public void setCircled(boolean circled) {
		this.circled = circled;
	}

	public Token getToken() {
		return token;
	}

	public void setToken(Token token) {
		this.token = token;
	}

	public UIPartition getPartition() {
		return partition;
	}

	public void setPartition(UIPartition partition) {
		this.partition = partition;
	}

	// Methods

	public Figure getFigure() {
		if (token != null) {
			return token.getFigure();
		} else {
			return null;
		}
	}

	public void circle(Graphics g) {
		if (circled) {
			Color def = g.getColor();
			g.setColor(Color.BLUE);
			g.drawRect(x, y, width, height);
			g.setColor(def);
		}
	}

	public void paintLabel(Graphics g, int h) {
		if (label != null) {
			Color color = g.getColor();
			g.setColor(labelColor);
			g.setFont(new Font("Arial", Font.ITALIC, 16));
			g.drawChars(label.toCharArray(), 0, label.length(), x, h
					+ partition.getHeight() - 3 * partition.getInterval());
			g.setColor(color);
		}
	}

	public void addX(int add) {
		int y = (int) getY();
		int x = (int) getX();
		setLocation(x + add, y);
	}

	@Override
	public int compareTo(UIMusicComponent c) {
		double placeDiff = x - c.getX();

		return (int) (placeDiff);
	}

	public Rectangle getVisibleRect() {

		Rectangle nextPart = new Rectangle(x, y, width, height + 200);
		return nextPart;
	}

}
